package com.mtons.mblog.modules.service.impl;

import com.mtons.mblog.base.utils.BeanMapUtils;
import com.mtons.mblog.modules.data.AnswerFavoriteVO;
import com.mtons.mblog.modules.data.AnswerVO;
import com.mtons.mblog.modules.data.FavoriteVO;
import com.mtons.mblog.modules.data.PostVO;
import com.mtons.mblog.modules.entity.AnswerFavorite;
import com.mtons.mblog.modules.entity.Favorite;
import com.mtons.mblog.modules.repository.AnswerFavoriteRepository;
import com.mtons.mblog.modules.service.AnswerFavoriteService;
import com.mtons.mblog.modules.service.AnswerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.*;

@Slf4j
@Service
@Transactional(readOnly = true)
public class AnswerFavoriteServiceImpl implements AnswerFavoriteService {

    @Autowired
    private AnswerFavoriteRepository answerFavoriteRepository;
    @Autowired
    private AnswerService answerService;

    @Override
    public Page<AnswerFavoriteVO> pagingByUserId(Pageable pageable, long userId) {
        Page<AnswerFavorite> page = answerFavoriteRepository.findAllByUserId(pageable, userId);

        List<AnswerFavoriteVO> rets = new ArrayList<>();
        Set<Long> answerIds = new HashSet<>();
        for (AnswerFavorite po : page.getContent()) {
            rets.add(BeanMapUtils.copy(po));
            answerIds.add(po.getAnswerId());
        }

        if (answerIds.size() > 0) {
            Map<Long, AnswerVO> answers = answerService.findMapByIds(answerIds);

            for (AnswerFavoriteVO t : rets) {
                AnswerVO p = answers.get(t.getAnswerId());
                t.setAnswer(p);
            }
        }
        return new PageImpl<>(rets, pageable, page.getTotalElements());
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(long userId, long answerId) {
        AnswerFavorite po = answerFavoriteRepository.findByUserIdAndAnswerId(userId, answerId);

        Assert.isNull(po, "您已经收藏过此回答");

        // 如果没有喜欢过, 则添加记录
        po = new AnswerFavorite();
        po.setUserId(userId);
        po.setAnswerId(answerId);
        po.setCreated(new Date());

        answerFavoriteRepository.save(po);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void delete(long userId, long answerId) {
        AnswerFavorite po = answerFavoriteRepository.findByUserIdAndAnswerId(userId, answerId);
        Assert.notNull(po, "还没有喜欢过此回答");
        answerFavoriteRepository.delete(po);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteByAnswerId(long answerId) {
        int rows = answerFavoriteRepository.deleteByAnswerId(answerId);
        log.info("answerFavoriteRepository delete {}", rows);
    }
}
